package com.hrzyit.base.ui.activity

import android.os.Bundle
import android.util.Log
import com.alibaba.android.arouter.launcher.ARouter
import com.hrzyit.base.common.BaseApplication
import com.hrzyit.base.injection.component.ActivityComponent
import com.hrzyit.base.injection.component.DaggerActivityComponent
import com.hrzyit.base.injection.module.ActivityModule
import com.hrzyit.base.injection.module.LifecycleProviderModule
import com.hrzyit.base.presenter.BasePresenter
import com.hrzyit.base.presenter.view.BaseView
import com.hrzyit.base.utils.DevicesUtils
import com.hrzyit.base.widgets.ProgressLoading
import org.greenrobot.eventbus.EventBus
import org.jetbrains.anko.toast
import javax.inject.Inject

/**
 * MVP 模式的 Activity 基础类
 * @author YangWei
 * @date 2019/3/21
 */
abstract class BaseMvpActivity<T: BasePresenter<*>>:BaseActivity(), BaseView {

    /**
     * 泛型，Dagger注入
     */
    @Inject
    lateinit var mPresenter: T

    lateinit var mActivityComponent:ActivityComponent

    private lateinit var mProgressDialog: ProgressLoading

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        // Dagger2 Component 注册
        initActivityInjection()
        injectComponent()

        // 查看设备信息
        Log.v("屏幕信息：宽度、高度、密度、dpi", DevicesUtils.getDeviceScreenInfo(this).joinToString(","))

        // 初始化自定义加载弹框
        mProgressDialog = ProgressLoading.create(this)

        // ARouter 注册
        ARouter.getInstance().inject(this)
    }

    /**
     * 初始Activity Component
     */
    private fun initActivityInjection() {
        mActivityComponent = DaggerActivityComponent.builder()
            .appComponent((application as BaseApplication)
            .appComponent)
                // 生命周期管理
            .activityModule(ActivityModule(this))
            .lifecycleProviderModule(LifecycleProviderModule(this))
            .build()
    }

    /**
     * Dagger2 注册
     */
    protected abstract fun injectComponent()

    /**
     * 继承类初始化视图方法
     */
//    protected abstract fun initView()

    /**
     * 显示加载框，默认实现
     */
    override fun showLoading() {
        mProgressDialog.showLoading()
    }

    /**
     * 隐藏加载框，默认实现
     */
    override fun hideLoading() {
        mProgressDialog.hideLoading()
    }

    /**
     * 错误信息提示，默认实现
     */
    override fun onError(text: String) {
        toast(text)
    }

}